//
//  STXYChooserViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/21.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "STXYChooserViewModel.h"

#import "STXYChooserCellModel.h"

@interface STXYChooserViewModel()

@property (nonatomic, readwrite, strong) RACCommand* requestDataCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;

@end

@implementation STXYChooserViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    self.cellHeight = 44.0f;
    
    self.lastPage = 100;
    self.shouldMultiSections = NO;
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = YES;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = NO;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = NO;
    
    /// 选中
    self.selectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath* indexPath)
    {
        @strongify(self);
        
        STXYChooserCellModel* itemViewModel = self.dataSource[indexPath.row];
        
        for (NSInteger index=0; index<self.dataSource.count; index++)
        {
            STXYChooserCellModel* cellModel = self.dataSource[index];
            
            bool isChoose = cellModel == itemViewModel;
            
            cellModel.isChoose = isChoose;
            
            if (isChoose)
            {
                DataManager* dm = [DataManager sharedDataManager];
                
                DeclarationInfosData* infos = dm.declarationInfos;
                DeclarationInfoData* curInfo = infos.datas[index];
                
                self.strText = curInfo.content;
            }
        }
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
                [subscriber sendNext:itemViewModel];
                [subscriber sendCompleted];
            });
            return nil;
        }];
    }];
    /// 允许并行执行
    self.selectCmd.allowsConcurrentExecution = NO;
}

- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page
{
    @weakify(self)
    
    DataManager* dm = [DataManager sharedDataManager];
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self)
        
        DeclarationInfosData* data = dm.declarationInfos;
        
        /// 转化数据
        @weakify(self);
        NSArray *viewModels = [data.datas.rac_sequence map:^STXYChooserCellModel *(DeclarationInfoData* curInfoData) {
            @strongify(self);
            
            //计算当前cellheight
            STXYChooserCellModel *itemViewModel = [[STXYChooserCellModel alloc] initWithData:curInfoData cellHeight:self.cellHeight];
            
            return itemViewModel;
        }].array;
        
        // 选中第一个
//        if (viewModels.count > 0)
//        {
//            STXYChooserCellModel* cellModel = viewModels[0];
//            cellModel.isChoose = YES;
//        }
        
        self.dataSource = viewModels;
        
        [subscriber sendNext:nil];
        [subscriber sendCompleted];
        
        return nil;
    }];
}

- (BOOL)finishChoose
{
    if (self.strText && [self.strText isNotBlank])
    {
        self.chooseBlock(self.strText);
        
        return YES;
    }
    
    return NO;
}

@end
